package com.nbcio.modules.crm.contact.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nbcio.common.core.domain.R;
import com.nbcio.modules.crm.contact.domain.CrmContact;
import com.nbcio.modules.crm.contact.service.ICrmContactService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * CRM联系人管理控制器
 */
@Tag(name = "CRM联系人管理")
@RestController
@RequestMapping("/crm/contact")
@RequiredArgsConstructor
public class CrmContactController {

    private final ICrmContactService contactService;

    /**
     * 分页查询联系人
     */
    @GetMapping("/page")
    public R<IPage<CrmContact>> page(
            @RequestParam int pageNum,
            @RequestParam int pageSize,
            @RequestParam(required = false) Long customerId,
            @RequestParam(required = false) String contactName,
            @RequestParam(required = false) String phone,
            @RequestParam(required = false) String roleType,
            @RequestParam(required = false) Integer isPrimary,
            @RequestParam(required = false) String status) {
        LambdaQueryWrapper<CrmContact> qw = new LambdaQueryWrapper<>();
        if (customerId != null) {
            qw.eq(CrmContact::getCustomerId, customerId);
        }
        if (contactName != null && !contactName.isEmpty()) {
            qw.like(CrmContact::getContactName, contactName);
        }
        if (phone != null && !phone.isEmpty()) {
            qw.like(CrmContact::getPhone, phone);
        }
        if (roleType != null && !roleType.isEmpty()) {
            qw.eq(CrmContact::getRoleType, roleType);
        }
        if (isPrimary != null) {
            qw.eq(CrmContact::getIsPrimary, isPrimary);
        }
        if (status != null && !status.isEmpty()) {
            qw.eq(CrmContact::getStatus, Integer.parseInt(status));
        }
        qw.orderByDesc(CrmContact::getIsPrimary);
        qw.orderByDesc(CrmContact::getCreateTime);
        IPage<CrmContact> page = contactService.page(Page.of(pageNum, pageSize), qw);
        return R.ok(page);
    }

    /**
     * 根据客户ID查询联系人列表
     */
    @GetMapping("/customer/{customerId}")
    public R<List<CrmContact>> listByCustomer(@PathVariable("customerId") String customerId) {
        LambdaQueryWrapper<CrmContact> qw = new LambdaQueryWrapper<>();
        qw.eq(CrmContact::getCustomerId, Long.parseLong(customerId));
        qw.eq(CrmContact::getStatus, 1);
        qw.orderByDesc(CrmContact::getIsPrimary);
        qw.orderByDesc(CrmContact::getCreateTime);
        List<CrmContact> list = contactService.list(qw);
        return R.ok(list);
    }

    /**
     * 根据ID查询联系人
     */
    @GetMapping("/{id}")
    public R<CrmContact> get(@PathVariable("id") String id) {
        CrmContact contact = contactService.getById(Long.parseLong(id));
        return R.ok(contact);
    }

    /**
     * 新增联系人
     */
    @PostMapping
    public R<Boolean> create(@RequestBody CrmContact contact) {
        return R.ok(contactService.save(contact));
    }

    /**
     * 更新联系人
     */
    @PutMapping("/{id}")
    public R<Boolean> update(@PathVariable("id") String id, @RequestBody CrmContact contact) {
        contact.setContactId(Long.parseLong(id));
        return R.ok(contactService.updateById(contact));
    }

    /**
     * 删除联系人
     */
    @DeleteMapping("/{id}")
    public R<Boolean> remove(@PathVariable("id") String id) {
        return R.ok(contactService.removeById(Long.parseLong(id)));
    }

    /**
     * 批量删除联系人
     */
    @PostMapping("/batch-delete")
    public R<Boolean> batchDelete(@RequestBody List<String> ids) {
        List<Long> longIds = ids.stream().map(Long::parseLong).collect(Collectors.toList());
        return R.ok(contactService.removeByIds(longIds));
    }
}

